# -*- coding:utf-8 -*-
from wmcloud_dataclient import Client
from cassandra.cluster import Cluster
from cassandra import ConsistencyLevel
from cassandra.query import BatchStatement, BatchType
from datetime import *
import traceback
import json
import os
import time

def capture_wmcloud_forward_main_force_and_successional_contract_raw_data(forward_contractobject_list=[], try_count=3):
    client = Client('93ef5ebba60c5f1da190801750612423db818daad66155b6b191417b19d331b9')
    cluster = Cluster(protocol_version=4)
    session = cluster.connect()
    session.set_keyspace('wmcloud')
    insert_forward = session.prepare('insert into wmcloud.forward_m_s_contract_daily_transaction_info (contractobject, tradedate, ticker, secid, secshortname, exchangecd, chg, chg1, chgpct, closeprice, contractmark, highestprice, lowestprice, maincon, openint, openprice, precloseprice, presettleprice, secshortnameen, settleprice, smaincon, turnovervalue, turnovervol) values (?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?)')
    end_datetime = datetime.today()
    weekend = int(end_datetime.strftime('%u')) - 5
    if weekend > 0:
        end_datetime = end_datetime - timedelta(days=weekend)
    end_time = str(end_datetime.strftime('%Y%m%d'))
    for forward_contractobject in forward_contractobject_list:
        row = session.execute("select tradedate from wmcloud.forward_m_s_contract_daily_transaction_info where contractobject='%s' order by tradedate desc limit 1" % (forward_contractobject))
        row_l = list(row)
        if len(row_l) > 0:
            if row_l[0].tradedate.strftime('%Y%m%d') == end_time:
                # 说明已经下载了最新数据了不需要进一步的下载
                continue
            sdatet = row_l[0].tradedate + timedelta(days = 1)
            start_time = sdatet.strftime('%Y%m%d')
        else:
            start_time = '19970101'
        # 使用远程数据进行查询
        for maincon in range(2):
            url = '/api/market/getMktMFutd.json?field=&mainCon=%d&contractMark=&startDate=%s&endDate=&contractObject=%s' % (maincon, start_time, forward_contractobject)
            for try_c in range(try_count):
                try:
                    code, result = client.getData(url)
                    if code == 200:
                        forward_json = json.loads(result)
                        print(forward_contractobject)
                        if 'retCode' not in forward_json:
                            print('error json : ',forward_json)
                        elif forward_json['retCode'] == 1:
                            batch_stmt = BatchStatement(batch_type = BatchType.UNLOGGED, consistency_level = ConsistencyLevel.QUORUM)
                            for inx, data in enumerate(forward_json['data']):
                                # Save it to cassandra (scylladb)
                                # 这里主要是把时间调整到下午4点这个时候都收盘了
                                trade_date_timestamp = datetime.strptime(data['tradeDate'], '%Y-%m-%d') + timedelta(hours = 16)
                                batch_stmt.add(insert_forward, (data["contractObject"], trade_date_timestamp, data["ticker"], data["secID"] if "secID" in data else "unknown", data["secShortName"] if "secShortName" in data else "unknown", data["exchangeCD"] if "exchangeCD" in data else "unknown", data["chg"] if "chg" in data else 0.0, data["chg1"] if "chg1" in data else 0.0, data["chgPct"] if "chgPct" in data else 0.0, data["closePrice"] if "closePrice" in data else 0.0, data["contractMark"] if "contractMark" in data else "unknown", data["highestPrice"] if "highestPrice" in data else 0.0, data["lowestPrice"] if "lowestPrice" in data else 0.0, data["mainCon"] if "mainCon" in data else -1, data["openInt"] if "openInt" in data else 0.0, data["openPrice"] if "openPrice" in data else 0.0, data["preClosePrice"] if "preClosePrice" in data else 0.0, data["preSettlePrice"] if "preSettlePrice" in data else 0.0, data["secShortNameEN"] if "secShortNameEN" in data else "unknown", data["settlePrice"] if "settlePrice" in data else 0.0, data["smainCon"] if "smainCon" in data else 0, data["turnoverValue"] if "turnoverValue" in data else 0.0, data["turnoverVol"] if "turnoverVol" in data else 0.0))
                                if inx != 0 and inx % 20 == 0:
                                    session.execute(batch_stmt)
                                    batch_stmt = BatchStatement(batch_type = BatchType.UNLOGGED, consistency_level = ConsistencyLevel.QUORUM)
                            else:
                                session.execute(batch_stmt)
                        break
                except Exception as e:
                    print(e)
                    continue

    session.shutdown()
    cluster.shutdown()


def get_forward_contractobject_list():
    # contractobject
    cluster = Cluster(protocol_version=4)
    session = cluster.connect()
    session.set_keyspace('wmcloud')
    rows = session.execute('select contractobject from forward_basic_info')
    forward_contractobject_list = []
    for row in rows:
        forward_contractobject_list.append(row.contractobject)

    session.shutdown()
    cluster.shutdown()
    return forward_contractobject_list

if __name__ == '__main__':
    try:
        forward_contractobject_list = get_forward_contractobject_list()
        capture_wmcloud_forward_main_force_and_successional_contract_raw_data(forward_contractobject_list)
    except Exception as e:
        traceback.print_exc()
